package cn.itsource.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.util.PageList;
import cn.itsource.pet.domain.*;
import cn.itsource.pet.mapper.PetTypeMapper;
import cn.itsource.pet.mapper.SearchMasterMessageMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.SearchMasterMessageService;
import cn.itsource.pet.service.SearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class SearchMasterMsgServiceImpl implements SearchMasterMsgService {
    @Autowired
    SearchMasterMessageMapper searchMasterMessageMapper;
    @Autowired
    PetTypeMapper petTypeMapper;
    @Autowired
    SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public PageList<SearchMasterMsg> pagelist(SearchMasterMsgQuery query) {
        //查询数据
        PageList<SearchMasterMsg> messagePageList = searchMasterMsgMapper.pagelist(query);
        messagePageList.getList().forEach(message -> {
            //拿到宠物类型id
            Long petTypeId = message.getPetType();
            PetType petType = petTypeMapper.selectByPrimaryKey(petTypeId);
            if (Objects.nonNull(petType))  message.setPetTypeName(petType.getName());

        });
        return messagePageList;
    }

    @Override
    public AjaxResult shenghe(SearchMasterMsg message) {
        SearchMasterMessage searchMasterMessage = searchMasterMessageMapper.selectByPrimaryKey(message.getId());
        if(Objects.isNull(searchMasterMessage)){
            SearchMasterMsg masterMsg = searchMasterMsgMapper.selectByPrimaryKey(message.getId());
            Optional.ofNullable(masterMsg).orElseThrow(() -> new RuntimeException("寻主信息不存在,请重新选择"));
            SearchMasterMessage masterMessage = BeanUtil.copyProperties(masterMsg, SearchMasterMessage.class);
            searchMasterMessageMapper.insertSelective(masterMessage);
        }
        message.setState(1);
        searchMasterMsgMapper.updateByPrimaryKeySelective(message);
        return AjaxResult.me();
    }


    @Override
    public void insert(SearchMasterMsg searchMasterMsg) {
        searchMasterMsgMapper.insertSelective(searchMasterMsg);
    }

    @Override
    public void update(SearchMasterMsg searchMasterMsg) {

    }

    @Override
    public PageList<SearchMasterMsg> findPage(BaseQuery baseQuery, String key) {
        return null;
    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void deleteBatch(List<Long> ids) {

    }

    @Override
    public List<SearchMasterMsg> findAll() {
        return null;
    }

    @Override
    public SearchMasterMsg findOne(Long id) {
        return null;
    }
}
